public static int MaxSum(int[] a) {
    int sum = 0;
    int i=0;
    int max = a[i];
    for (i = 0; i < a.length; i++) {
        sum = sum + a[i];
        if (sum < a[i]) {
            sum = a[i];
        }
        if (sum > max) {
            max = sum;
        }
    }
    return max;
}